#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "../ddxc.h"


#define NAME_SIZE 64
#define PASSWORD_SIZE 64
#define NO -1
#define WRONGNAME -2


typedef struct member{
    char name[NAME_SIZE];
    char password[PASSWORD_SIZE];
}membernode,*memberlist;


int dic_search(int sockfd,memberlist member,char* name){
    int prow=0,pcloum=0;
    if(name != NULL){
        if(sticky_packs_send_func(sockfd,name,STRING_TYPE,(strlen(name+INT_SIZE)+1)) == -1){
            printf("err");
            return -1;
        }
    }
    
    if(sticky_packs_recv_func(sockfd,&prow,INT_TYPE) == -1){
        return -1;
    }
    if(sticky_packs_recv_func(sockfd,&pcloum,INT_TYPE) == -1){
        return -1;
    }
    if(prow <= 0 && pcloum <=0 ){
        printf("没有找到\n");
        return prow;
    }
    // printf("prow = %d\n",prow);
    char* buf=(char*)malloc(BUF_SIZE);
    if(buf == NULL){
        perror("malloc err");
        return -1;
    }
    char* ch=(char*)malloc(BUF_SIZE);
    if(buf == NULL  || ch == NULL){
        perror("malloc err");
        free(buf);
        buf =NULL;
        return -1;
    }
    for(int i=0;i< prow;i++){
        for(int j=0;j<pcloum;j++){
            if(sticky_packs_recv_func(sockfd,buf,STRING_TYPE) == -1){
                free(buf);
                free(ch);
                buf=NULL,ch=NULL;
                return -1;
            }
            if(sticky_packs_recv_func(sockfd,ch,STRING_TYPE) == -1){
                free(buf);
                free(ch);
                buf=NULL,ch=NULL;
                return -1;
            }
            printf("%s = %s\n",buf,ch);
        }
        printf("\n");
    }
    free(buf);
    free(ch);
    buf=NULL,ch=NULL;
    return 0;

}

int home_page(int sockfd,memberlist member){
    char clean[64]="";
    char buf[32]="";
    int n=-1;
    while ( 1 ) {
        memset(buf,0,sizeof(buf));
        printf("**************************************************************\n"); 
        printf("1:查单词 2:查历史记录 3:退出\n"); 
        printf("**************************************************************\n"); 
        printf("please select : "); 
        if (scanf("%d", &n) != 1) { 
            fgets(clean, 64, stdin); 
            printf("\n"); 
            continue; 
        }
        if(sticky_packs_send_func(sockfd,&n,INT_TYPE,n) == -1){
            printf("err");
            return -1;
        }
        switch ( n ) {
            case 1 : //单词查询
                printf("请输入查询的单词:");
                scanf("%s",buf+INT_SIZE);
                if(dic_search(sockfd,member,buf) < 0){
                    return -1;
                }
                break; 
            case 2 ://历史记录查询
                if(dic_search(sockfd,member,NULL) < 0){
                    return -1;
                } 
                break; 
            case 3 : //退出
                // sqlite3_close(db); 
                // exit(0);
                return 0;
                break; 
            default: 
                printf("err option\n"); 
            break; 
        } 
    }
}

int denglu(int n,int sockfd,memberlist member){
    int temp=-1;
    printf("请输入帐号\n");
    scanf("%s",(member->name+INT_SIZE));
    if(member->name[INT_SIZE] <='9'&& member->name[INT_SIZE] >='0'){//防止错误发送错误表名
        temp =WRONGNAME;
    }else{
        temp =1;
    }
    if(sticky_packs_send_func(sockfd,&temp,INT_TYPE,temp) == -1){
        printf("err");
        return -1;
    }
    if(temp == WRONGNAME){
        return temp;
    }

    if(sticky_packs_send_func(sockfd,member->name,STRING_TYPE,(strlen(member->name+INT_SIZE)+1)) == -1){
        printf("err");
        return -1;
    }
    printf("请输入密码\n");
    scanf("%s",(member->password + INT_SIZE));
    if(sticky_packs_send_func(sockfd,member->password,STRING_TYPE,(strlen(member->password+INT_SIZE)+1)) == -1){
        printf("err");
        return -1;
    }

    if(sticky_packs_recv_func(sockfd,&temp,INT_TYPE) == -1){
        return -1;
    }
    
    return temp;
}

int login(int sockfd,memberlist member){
    char clean[64]="";
    int n=-1;
    if(sticky_packs_recv_func(sockfd,clean,STRING_TYPE) == -1){
        return -1;
    }
    printf("%s\n",clean);
    while ( 1 ) {
        printf("**************************************************************\n"); 
        printf("1: 登陆 2: 注册 3: 退出\n"); 
        printf("**************************************************************\n"); 
        printf("please select : "); 
        if (scanf("%d", &n) != 1) { 
            fgets(clean, 64, stdin); 
            printf("\n"); 
            continue; 
        }
        if(sticky_packs_send_func(sockfd,&n,INT_TYPE,n) == -1){
            printf("err");
            return -1;
        }
        switch ( n ) {
            case 1 : //登陆
                if(denglu(n,sockfd,member) == 1){
                    printf("登陆成功\n");
                    home_page(sockfd,member);
                }else{
                    printf("帐号密码错误!!!\n");
                }
                
                break; 
            case 2 : //注册
                {   
                    int ret=denglu(n,sockfd,member);
                    if(ret == 0){
                        printf("注册成功\n");
                    }else if(ret == 1){
                        printf("帐号已存在\n");
                    }else if(ret == WRONGNAME){
                        printf("帐号格式错误,不允许存在数字与符号\n");
                    }else{
                        printf("服务端出错\n");
                    }
                }
                break; 
            case 3 :  
                return 0;
                break;  
            default: 
                printf("err option\n"); 
            break; 
        } 
    }
}



int client_creat(int* sockfd){
    *sockfd = socket(AF_INET,SOCK_STREAM,0);//ipv4,tcp
    if(*sockfd == -1){
        perror("sock err");
        return -1;
    }
    //服务器地址:IP 端口号
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;
    // server_addr.sin_port =htons(atoi(argv[2]));
    server_addr.sin_port =htons(8888);

    // server_addr.sin_addr.s_addr=inet_addr(argv[1]);
    server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");

    printf("IP=%s\n",inet_ntoa(server_addr.sin_addr));
    socklen_t addrlen= sizeof(server_addr);
    

    //连接
    if(connect(*sockfd,(struct sockaddr*)&server_addr,addrlen) < 0){
        perror("coonet err");
        close(*sockfd);
        return -1;
    }
    printf("connet  success\n");

    return 0;
}



int main(int argc,char const *argv[]){
    // if(argc < 2){
    //     printf("usage err\n");
    //     return -1;
    // }
    int sockfd=-1;
    if(client_creat(&sockfd) < 0){
        return -1;
    }
    membernode node;
    login(sockfd,&node);
    

    //关闭
    close(sockfd);


    return 0;
}
